'''
Created on 2012-12-12

@author: levi
'''
import unittest

import time

from symbolic_state_space import SymbolicStateSpace

from t_core.matcher import Matcher

from transformation_patterns.HS2S import HS2S
from transformation_patterns.HM2M import HM2M
from transformation_patterns.HF2F import HF2F
from transformation_patterns.HSM2SM import HSM2SM
from transformation_patterns.HSF2SF import HSF2SF
from transformation_patterns.HMM2MM import HMM2MM
from transformation_patterns.HFF2FF import HFF2FF

from symbolic_exec_patterns.HMatch_S2SLHS import HMatch_S2SLHS
from symbolic_exec_patterns.HMatch_M2MLHS import HMatch_M2MLHS
from symbolic_exec_patterns.HMatch_F2FLHS import HMatch_F2FLHS

class Test(unittest.TestCase):

    def setUp(self):
        self.backwardPatterns = {'HS2S': None,
                                 'HM2M': None,  
                                 'HF2F': None,                 
                                 'HSM2SM': [Matcher(HMatch_S2SLHS()),Matcher(HMatch_M2MLHS())],
                                 'HMM2MM': [Matcher(HMatch_M2MLHS()),Matcher(HMatch_M2MLHS())],
                                 'HSF2SF': [Matcher(HMatch_S2SLHS()),Matcher(HMatch_F2FLHS())],
                                 'HFF2FF': [Matcher(HMatch_F2FLHS()),Matcher(HMatch_F2FLHS())]}


    def test_models_example(self):
        transformation = [[HS2S(), HM2M(), HF2F()], [HSM2SM(), HMM2MM(), HSF2SF(), HFF2FF()]]
        rulesIncludingBackLinks = [[], [transformation[1][0], transformation[1][1], transformation[1][2], transformation[1][3]]]
        nrStates = self._run(transformation, rulesIncludingBackLinks, self.backwardPatterns)  
        self.assertEqual(nrStates,34)
        

#    def test_many_rules_1(self):
#        transformation = [[HS2S(), HM2M(), HF2F(), HS2S(), HM2M(), HF2F()], [HSM2SM(), HMM2MM(), HSF2SF(), HFF2FF()]]
#        rulesIncludingBackLinks = [[], [transformation[1][0], transformation[1][1], transformation[1][2], transformation[1][3]]]
#        nrStates = self._run(transformation, rulesIncludingBackLinks, self.backwardPatterns)  
#        self.assertEqual(nrStates,34)
#
#    def test_many_rules_2(self):
#        transformation = [[HS2S(), HM2M(), HF2F(), HS2S(), HM2M(), HF2F()], [HSM2SM(), HMM2MM(), HSF2SF(), HFF2FF(), HSM2SM(), HMM2MM()]]
#        rulesIncludingBackLinks = [[], [transformation[1][0], transformation[1][1], transformation[1][2], transformation[1][3]]]
#        nrStates = self._run(transformation, rulesIncludingBackLinks, self.backwardPatterns)  
#        self.assertEqual(nrStates,34)
#        
#    def test_many_rules_3(self):
#        transformation = [[HS2S(), HM2M(), HF2F(), HS2S(), HM2M(), HF2F()], [HSM2SM(), HMM2MM(), HSF2SF(), HFF2FF(), HSM2SM(), HMM2MM()], [HS2S(), HM2M(), HSM2SM()]]
#        rulesIncludingBackLinks = [[], [transformation[1][0], transformation[1][1], transformation[1][2], transformation[1][3]], [transformation[2][2]]]
#        nrStates = self._run(transformation, rulesIncludingBackLinks, self.backwardPatterns)  
#        self.assertEqual(nrStates,34)

    def _run(self, transformation, rulesIncludingBackLinks, backwardPatterns):
        s = SymbolicStateSpace(transformation, rulesIncludingBackLinks, self.backwardPatterns)
        t0 = time.time()
        res = s.build_set()
        t1 = time.time()
        print '\n'
        print '-----------------------------------------------' 
        print 'Built ' + str(len(s.symbStateSpace)) + ' states'         
        print 'Done in: ' + str(t1 -t0)
        self._print_states(s)
        return len(s.symbStateSpace)
    
    def _print_states(self,s):
        for state in s.symbStateSpace:
            print "----------"
            if state == ():
                print 'Empty'
            else:
                for s in state:
                    print s


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()